#include "packetlist.h"
#include "mainapp.h"
#include <QDir>
#include <QMessageBox>
//
PacketList::PacketList ( QObject *parent )
		: QObject ( parent ) {}

PacketList::~PacketList() {
	qDeleteAll ( packetList );
}


int PacketList::count() const {
	return packetList.count();
}

PacketItem * PacketList::getPacket (  int i ) {
	//return packetList.value ( i, NULL );
	return packetList.at ( i );
}

void PacketList::initPackets ( const QString &category ) {
	static const QString pdir ( "/usr/portage/" );
	qDeleteAll ( packetList );
	if ( category.isEmpty() ) {
		foreach ( QString line, MainApp::getCategories() ) {
			parsePackets ( pdir + line );
		}
	} else parsePackets ( pdir + category );
}

void PacketList::parsePackets ( const QString &category_dir ) {
	QDir cat_dir ( category_dir );
	if ( cat_dir.exists() ) {
		foreach ( QFileInfo val, cat_dir.entryInfoList ( QDir::Dirs | QDir::NoDotAndDotDot ) ) {
			packetList.append ( new PacketItem ( val.fileName() ) );
		}
	} else QMessageBox::warning ( MainApp::app->getMainWindow(), tr ( "Error" ), tr ( "No category named %1" ).arg ( cat_dir.dirName() ) );
}

//
